//
//  StressStateView.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/3.
//

import UIKit
import MYModule

class StressStateView: View {
    
    var stress: Int? {
        
        didSet {
            
            layoutSubviews()
            
        }
    }
    
    let markImageView = ImageView()
    
    let stateColorsView = StateColorsView()
    
    lazy var dottedLines: [DottedLine] = {
        
        var dottedLines: [DottedLine] = []
        
        (0..<3).forEach {_ in
            let line = DottedLine()
            dottedLines.append(line)
        }
        
        return dottedLines
    }()
    
    lazy var stateLabels: [Label] = {
        
        
        let stateLabels: [Label] = ["stress_relaxed".language,
                                    "stress_normal".language,
                                    "stress_medium".language,
                                    "stress_stressed".language].map({
            
            let label = Label()
            label.text = $0
            label.textColor = UIColor.assist_222222
            label.font = Fonts.systemFont(ofSize: 10)
            label.textAlignment = .center
            
            return label
        })
        
        return stateLabels
    }()
    
    lazy var progressLabels: [Label] = {
        
        let progressLabels: [Label] = [0, 30, 60, 80, 100].map({
            
            let label = Label()
            label.text = "\($0)"
            label.textColor = UIColor.assist_D0D0D0
            label.font = Fonts.systemFont(ofSize: 10)
            return label
        })
        
        return progressLabels
    }()
    
    override func makeUI() {
        super.makeUI()
        
        markImageView.layer.cornerRadius = 10
        markImageView.image = UIImage(named: "ic_home_today_stress_progress")
        markImageView.isHidden = true
        
        addSubview(stateColorsView)
        stateColorsView.addSubview(markImageView)
        
        progressLabels.forEach {
            self.addSubview($0)
        }
        
        stateLabels.forEach {
            self.addSubview($0)
        }
        
        dottedLines.forEach {
            self.addSubview($0)
        }
        
        stateColorsView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(24)
        }
        
        markImageView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            
        }
        
        
        let stateLabelsMultipliedRight = [0.85, 0.55, 0.3, 0.1]
        stateLabels.enumerated().forEach {i, label in

            let multiplied = 1 - stateLabelsMultipliedRight[i]

            label.snp.makeConstraints { make in
                make.top.equalTo(stateColorsView.snp.bottom).offset(4)
                make.centerX.equalTo(self.snp.right).multipliedBy(multiplied)

                make.bottom.equalToSuperview()

            }

        }

        let multipliedRight = [1.0, 0.7, 0.4, 0.2]
        progressLabels.enumerated().forEach {i, label  in

            label.snp.makeConstraints { make in

                make.top.equalTo(stateColorsView.snp.bottom).offset(4)

                if i == 0 {

                    make.centerX.equalTo(self.snp.left)

                } else if i == progressLabels.count - 1 {

                    make.centerX.equalTo(self.snp.right)

                } else {
                    
                    let multiplied = 1 - multipliedRight[i]
                    make.centerX.equalTo(self.snp.right).multipliedBy(multiplied)
                   
                }
            }
        }
        
       
        dottedLines.enumerated().forEach {
         
            let multiplied = 1 - multipliedRight[$0 + 1]
            $1.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.bottom.equalTo(stateColorsView.snp.bottom)
                make.centerX.equalTo(self.snp.right).multipliedBy(multiplied)
                make.width.equalTo(1.0)
            }
           

        }

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let stress = stress else {
            markImageView.isHidden = true
            return
        }
        markImageView.isHidden = stress == 0
        self.layoutIfNeeded()
        
        
        let multiplied = CGFloat(stress)/100.0
        var left = multiplied * self.frame.width
        
        if left <= markImageView.frame.width/2.0 {
            left = 0
        } else if left > self.frame.width - markImageView.frame.width {
            
            left = self.frame.width - markImageView.frame.width
        } else {
            
            left = left - markImageView.frame.width/2.0
            
        }
        
        
        markImageView.snp.remakeConstraints { make in

            if left == 0 {
                make.left.equalToSuperview()
                make.centerY.equalToSuperview()
            } else {
                
                
                
                make.left.equalToSuperview().offset(left)
                make.centerY.equalToSuperview()
            }

        }
    }
    
}

//MARK: - DottedLine
extension StressStateView {
    
    class DottedLine: View {
        
        let lineColor = UIColor.assist_FFFFFF
        
        let lineLayer = CAShapeLayer()
        
        
        override func makeUI() {
            super.makeUI()
            
            lineLayer.strokeColor = lineColor.cgColor
            lineLayer.frame = self.bounds
            lineLayer.lineWidth = 1.0
            lineLayer.lineDashPattern = [3.0, 3.0]
            
            layer.addSublayer(lineLayer)
            
        }
        
        var path: UIBezierPath {
            
            let path = UIBezierPath()
            path.move(to: CGPoint(x: self.bounds.midX, y: 0))
            path.addLine(to: CGPoint(x: self.bounds.midX, y: self.bounds.height))
            return path
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            
            lineLayer.frame = self.bounds
            lineLayer.path = path.cgPath
            
        }
        
    }
}

//MARK: - StateColorsView
extension StressStateView {
    
    class StateColorsView: View {
        
        private let colors = [UIColor.Stress.theme,
                              UIColor.Stress.medium,
                              UIColor.Stress.stressed
        ]
        private let locations: [NSNumber] = [0, 0.53, 1]
        
        let colorsLayers = CAGradientLayer()
        
        override func makeUI() {
            super.makeUI()
            
            
            colorsLayers.colors = colors.map{ $0.cgColor }
            colorsLayers.locations = locations
            
            colorsLayers.locations = locations
            
            colorsLayers.startPoint = CGPoint(x: 0.25, y: 0.5)
            colorsLayers.endPoint = CGPoint(x: 0.75, y: 0.5)
            
            layer.addSublayer(colorsLayers)
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            colorsLayers.frame = self.bounds
            
            colorsLayers.cornerRadius = self.bounds.height/2.0
        }
        
        
    }
    
    
}



